package cn.com.xiaoshoujia.activities.controller;

import cn.com.xiaoshoujia.activities.dto.*;
import cn.com.xiaoshoujia.activities.service.ActivitiesJoinService;
import cn.com.xiaoshoujia.utils.Result;
import cn.com.xiaoshoujia.utils.SALES;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @author dulei
 * @Title: ActivitiesJoinController
 * @Description: 拼团用户包装类
 * @Time 2017/8/3 10:02
 */
@RestController
public class ActivitiesJoinController {

    Logger logger = LoggerFactory.getLogger(ActivitiesJoinController.class);

    @Autowired
    private ActivitiesJoinService activitiesJoinService;
    /**
     * @Author: dulei
     * @Description: 用户本参团，提供团信息
     * @Date: 2017/8/3 19:07
     */
    @GetMapping("team/userJoin")
    public Object findSysJoin(@Valid ActivityWxParamDTO activityWxParamDTO, BindingResult result, HttpServletRequest request, HttpServletResponse response){
        if (result.hasErrors()){
            logger.debug("error request");
            return SALES.encryption(request, response, new Result("FAIL", "错误请求",null));
        }
        ActivityUserTeamDTO userTeam = null;
        try {
            userTeam = activitiesJoinService.findUserJoin(activityWxParamDTO);
        } catch (UnsupportedEncodingException e) {
            logger.warn("utf-8 exception");
            return SALES.encryption(request, response, new Result("FAIL", "编码错误",null));
        }catch (RuntimeException e) {
            logger.error(e.getMessage());
            return SALES.encryption(request, response, new Result("FAIL", "系统错误",null));
        }
        return SALES.encryption(request, response, new Result("SUCCESS",userTeam));
    }
    /**
     * @Author: dulei
     * @Description: 用户未参团，提供链接团信息
     * @Date: 2017/8/3 19:07
     */
    @GetMapping("team/shareJoin")
    public Object shareJoin(@Valid ActivityWxParamDTO activityWxParamDTO,BindingResult result, HttpServletRequest request, HttpServletResponse response){
        if (result.hasErrors()){
            logger.debug("error request");
            return SALES.encryption(request, response, new Result("FAIL", "错误请求",null));
        }
        if (null==activityWxParamDTO.getHeadId()){
            logger.debug("error request");
            return SALES.encryption(request, response, new Result("FAIL", "错误请求",null));
        }
        ActivityShareTeamDTO shareTeam = null;
        try {
            shareTeam = activitiesJoinService.shareJoin(activityWxParamDTO);
        } catch (UnsupportedEncodingException e) {
            logger.warn("utf-8 exception");
            return SALES.encryption(request, response, new Result("FAIL", "编码错误",null));
        }
        return SALES.encryption(request, response, new Result("SUCCESS",shareTeam));
    }
    /**
     * @Author: dulei
     * @Description: 未支付接口
     * @param:  sysId 用户openId  id 拼团活动ID
     * @Date: 2017/8/5 17:51
     */
    @GetMapping("team/teamNoPay")
    public Object teamNoPay(String sysId,Long id,Integer type, HttpServletRequest request, HttpServletResponse response){
        if (null==id || StringUtils.isBlank(sysId) || (type.intValue()!=0&&type.intValue()!=1)){
            logger.debug("bad request");
            return SALES.encryption(request, response, new Result("FAIL", "错误请求",null));
        }
        ActivityOrderJoinDTO orderJoinDTO = activitiesJoinService.teamNoPay(sysId,id,type);
        if (null!=orderJoinDTO){
            try {
                orderJoinDTO.setValueJson(URLDecoder.decode(orderJoinDTO.getValueJson(), "utf-8"));
            } catch (UnsupportedEncodingException e) {
                logger.error("获取表单信息解码错误 sysId = {},id = {}, type = {},msg = {}",sysId,id,type,orderJoinDTO);
                return SALES.encryption(request, response, new Result("FAIL", "错误请求",null));
            }
        }
        return SALES.encryption(request, response, new Result("SUCCESS",orderJoinDTO));
    }
}
